<?php
/**
 * Bdat Site development
 * @author Will Skates
 */

/*----------------------------------------------------------------------------------------------------*/

/**
 * Outline of properties and functionality required to make and use content in the system which is used 
 * to populate masonry blocks. In this tweet data blocks.
 * @author Will Skates
 */
require_once('aggregatedcontent.php');
class Tweet extends aggregatedcontent {
	
	//Define where we can find tweet data in the database.
	public $ctable = 'tweets';
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Construct an sql query to get the data for events from the database. (This version of the function includes a link to the users twitter profile).
	 * @see BlockModel::getQuery()
	 */
	public function getQuery()
	{
		return 'SELECT ' . $this->ctable . '.uid,
						title,
						content,
						user_profiles.name,
						user_profiles.link,
						gradyears.date AS gradyear,sticky,
						user_profiles.twitter AS profileLink
						FROM ' . $this->ctable . ' 
				LEFT JOIN user_profiles ON user_profiles.userid = ' . $this->ctable . '.author
				LEFT JOIN gradyears ON gradyears.uid = user_profiles.gradyear
				WHERE published = 1 AND approved = 1';
	}
	
	/*----------------------------------------------------------------------------------------------------*/

	/**
	 * Get all data from the database for content of this type (and parse the content to remove the twitter username).
	 * @return	Array	- Array containing all of the content for this item.
	 */
	public function getAll()
	{
	
		$query = $this->db->query($this->getQuery( $this->ctable ));
		$this->load->helper('twitter');
		
		$res = $query->result();
		$c = 2;
		foreach($res as $k=>$v) {
			$res[$k]->listNum 	= $k;
			$parts = explode(':',$v->content,$c);
			$res[$k]->user 		= $parts[0];
			if ( isset($parts[1]) ) {
				$res[$k]->content 	= $parts[1];
			} else {
				$res[$k]->content 	= '';
			}
			
			//Fix the eventuality where they haven't typed in the full url. (see twitter helper).
			$res[$k]->profileLink = makeLink($res[$k]->profileLink);
		}
		
		return $query->result();
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
}